<?php

namespace App\Http\Service;

use App\Util\Result;
use App\Model\Comment;
use Illuminate\Support\Facades\DB;

trait ServiceComment{

    /**
     * czs
     * @return mixed
     * 评论列表
     */
    public static function getList($keyWord,$limit){
        $model = DB::table('comments as a');
        if (!empty($keyWord)) {
            $model->where("a.content", "like", '%' . $keyWord . '%');
        }
        $list = $model->leftJoin('members as b', 'b.id', '=', 'a.member_id')
            ->leftJoin('Items as c', 'c.SerialNo', '=', 'a.bid')
            ->leftjoin('circ_marc as d','d.MarcID','=','c.MarcID')
            ->orderBy('a.id','desc')
            ->select('a.id','a.content', 'a.source', 'b.nickname','c.Barcode','d.Title as title','a.site','a.created_at')
            ->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = Comment::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * 查看评论
     * czs
     */
    public static function view($id) {
        $res = DB::table('comments as a')
            ->where('a.id',$id)
            ->leftJoin('members as b', 'b.id', '=', 'a.member_id')
            ->leftJoin('infos as c', 'c.id', '=', 'a.map_id')
            ->select('a.content',  'b.nickname','c.title','a.site','a.created_at')
            ->first();
        return Result::success()->setData($res);
    }

    /**
     * web添加记录
     * czs
     */
    public static function addComment($content,$id,$memberId,$site) {
        $data['member_id'] = $memberId;
        $data['map_id'] = $id;
        $data['content'] = $content;
        $data['site'] = $site;
        $data['created_at'] = time();
        DB::table('comments')->insert($data);
    }

    /**
     * web资讯评论列表
     * czs
     */
    public static function commentList($id,$site) {
        $res = DB::table('comments as a')
            ->where('a.map_id',$id)
            ->where('a.site',$site)
            ->leftJoin('members as b','b.id','=','a.member_id')
            ->orderBy('a.id','desc')
            ->select('a.*','b.nickname','b.account','b.head','b.avatar')
            ->get();
        return Result::success()->setData($res);
    }
}


